<template>
  <el-dialog :title="!dataForm.id ? '新增' : '修改'" :close-on-click-modal="false" :visible.sync="visible">
    <el-form :model="dataForm" :rules="dataRule" ref="dataForm" @keyup.enter.native="dataFormSubmit()" label-width="80px">
      <el-form-item label="学院" prop="college">
        <el-input v-model="dataForm.college" placeholder="学院"></el-input>
      </el-form-item>
      <el-form-item label="结题、延期表id" prop="conclusionId">
        <el-input v-model="dataForm.conclusionId" placeholder="结题、延期表id"></el-input>
      </el-form-item>
      <el-form-item label="结题或延期" prop="conclusionPostpone">
        <el-input v-model="dataForm.conclusionPostpone" placeholder="结题或延期"></el-input>
      </el-form-item>
      <el-form-item label="验收结果（验收合格：1，验收不合格：2，同意延期：3，不同意延期：4）" prop="auditResult">
        <el-input v-model="dataForm.auditResult" placeholder="验收结果（验收合格：1，验收不合格：2，同意延期：3，不同意延期：4）"></el-input>
      </el-form-item>
      <el-form-item label="创建时间" prop="createTime">
        <el-input v-model="dataForm.createTime" placeholder="创建时间"></el-input>
      </el-form-item>
    </el-form>
    <span slot="footer" class="dialog-footer">
      <el-button @click="visible = false">取消</el-button>
      <el-button type="primary" @click="dataFormSubmit()">确定</el-button>
    </span>
  </el-dialog>
</template>

<script>
export default {
  data() {
    return {
      visible: false,
      dataForm: {
        id: 0,
        college: '',
        conclusionId: '',
        conclusionPostpone: '',
        auditResult: '',
        createTime: ''
      },
      dataRule: {
        college: [
          { required: true, message: '学院不能为空', trigger: 'blur' }
        ],
        conclusionId: [
          { required: true, message: '结题、延期表id不能为空', trigger: 'blur' }
        ],
        conclusionPostpone: [
          { required: true, message: '结题或延期不能为空', trigger: 'blur' }
        ],
        auditResult: [
          { required: true, message: '验收结果（验收合格：1，验收不合格：2，同意延期：3，不同意延期：4）不能为空', trigger: 'blur' }
        ],
        createTime: [
          { required: true, message: '创建时间不能为空', trigger: 'blur' }
        ]
      }
    }
  },
  methods: {
    init(id) {
      this.dataForm.id = id || 0
      this.visible = true
      this.$nextTick(() => {
        this.$refs['dataForm'].resetFields()
        if (this.dataForm.id) {
          this.$http({
            url: this.$http.adornUrl(`/sys/schoolresult/info/${this.dataForm.id}`),
            method: 'get',
            params: this.$http.adornParams()
          }).then(({ data }) => {
            if (data && data.code === 0) {
              this.dataForm.college = data.schoolResult.college
              this.dataForm.conclusionId = data.schoolResult.conclusionId
              this.dataForm.conclusionPostpone = data.schoolResult.conclusionPostpone
              this.dataForm.auditResult = data.schoolResult.auditResult
              this.dataForm.createTime = data.schoolResult.createTime
            }
          })
        }
      })
    },
    // 表单提交
    dataFormSubmit() {
      this.$refs['dataForm'].validate((valid) => {
        if (valid) {
          this.$http({
            url: this.$http.adornUrl(`/sys/schoolresult/${!this.dataForm.id ? 'save' : 'update'}`),
            method: 'post',
            data: this.$http.adornData({
              'id': this.dataForm.id || undefined,
              'college': this.dataForm.college,
              'conclusionId': this.dataForm.conclusionId,
              'conclusionPostpone': this.dataForm.conclusionPostpone,
              'auditResult': this.dataForm.auditResult,
              'createTime': this.dataForm.createTime
            })
          }).then(({ data }) => {
            if (data && data.code === 0) {
              this.$message({
                message: '操作成功',
                type: 'success',
                duration: 1500,
                onClose: () => {
                  this.visible = false
                  this.$emit('refreshDataList')
                }
              })
            } else {
              this.$message.error(data.msg)
            }
          })
        }
      })
    }
  }
}
</script>
